﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using SRW;
using System.Collections.Generic;
using SRW.BLL;

public partial class zxlx : System.Web.UI.UserControl
{
    public List<Wtproblem> mList = null;
    public string AnserRith = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //初始化时钟
            TimeSs.Text = "60";
            TimeMm.Text = "39";
            //体育专项申明
            LabType.Text = "现在系统已经为你抽取有关" + Session["stuproType"] + "的试题！" + "(提醒：在线练习以你的第一次选择的答案为准！)";
            LinkButton3.Visible = false;
            Label3.Visible = false;
            //记录成绩
            Session["selMark"] = 0;
            Session["PanDMark"] = 0;
            Session["IsEqual"] = -1;
        }
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            LinkButton1.Visible = false;
            LinkButton3.Visible = true;
            Label3.Visible = true;
            RBL1.Enabled = false;
        }
        Labmy.Visible = false;
        Label2.Visible = false;
        Finishtimu.Visible = false;
        if (Request.QueryString["Classid"] != null)
        {
            if (Request.QueryString["Classid"].ToString() == "2")
            {
                if (Session["ProSeList"] == null)
                {
                    timu.Text = "选择题";
                    shuoming.Text = "共40题选择题，每题2分！(提醒：在做题过程中请勿刷新页面！)";

                    mList = getListPro("选择题", 40);
                    if (mList == null)
                    {
                        JScript.AlertAndRedirect("题库没有足够的题目，无法抽题！", "stuks.aspx");
                        return;
                    }
                    Session["ProSeList"] = mList;
                    Session["SeIndex"] = 1;
                    if (Session["SeIndex"] != null && mList != null)
                    {
                        LbTitle.Text = "第" + ((int)Session["SeIndex"]) + "题：" + mList[(int)Session["SeIndex"] - 1].wtproname;

                        RBL1.DataTextField = "wtanname";
                        RBL1.DataValueField = "wtanname";
                        RBL1.DataSource = SQL.GetDataTable("wtanswer", "select wtanname from wtanswer where problemID=" + mList[(int)Session["SeIndex"] - 1].pkID + "");
                        RBL1.DataBind();
                    }
                    Lb_right.Text = "【请先选择答案！】";
                }
            }
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            TimeMm.Text = "0";
            TimeSs.Text = "0";
            TimeMm.UpdateAfterCallBack = true;
            TimeSs.UpdateAfterCallBack = true;
            return;
        }
        TimeSs.Text = Convert.ToString(int.Parse(TimeSs.Text) - 1);
        if (int.Parse(TimeSs.Text) < 0)
        {
            TimeMm.Text = Convert.ToString(int.Parse(TimeMm.Text) - 1);
            TimeSs.Text = "59";
            TimeMm.UpdateAfterCallBack = true;
            //TimeSs.UpdateAfterCallBack = true;
        }
        TimeSs.UpdateAfterCallBack = true;
    }
    private List<Wtproblem> getListPro(string type, int number)
    {
        //获取一份试题
        int sinoID = 0;
        if (Session["stuproType"] != null)
        {
            sinoID = int.Parse(SQL.GetData("select pkID from sinopro where sinoname='" + Session["stuproType"].ToString() + "'", "pkID"));
        }
        else
        {
            return null;
        }
        List<Wtproblem> mSelect = bWtproblem.selectList("select * from wtproblem where wtprotype='" + type + "' and pkID in(select problemID from problemsino where sinoID=" + sinoID + ")");
        if (mSelect.Count < 40 || mSelect.Count < 20)
        {
            return null;
        }
        
        List<Wtproblem> mSeTiMu = new List<Wtproblem>();//被选中
        //使用简单的随机抽取
        Random rnd = new Random();
        ArrayList ranold = new ArrayList();
        bool flag = false;
        for (int i = 0; i < number; )
        {
            int radnew = rnd.Next(mSelect.Count);
            for (int j = 0; j < ranold.Count; j++)
            {
                if (radnew.ToString().Equals(ranold[j].ToString()))
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                flag = false;
                continue;
            }
            else
            {
                flag = false;
                ranold.Add(radnew);
                i++;
            }
            mSeTiMu.Add(mSelect[radnew]);
        }
        return mSeTiMu;
    }
    protected void RBL1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            return;
        }
        if (Session["ProSeList"] != null && Session["SeIndex"] != null)
        {
            mList = (List<Wtproblem>)Session["ProSeList"];
        }
        else
        {
            return;
        }
        string rightname = SQL.GetData("select rightname from rightan where problemID=" + mList[(int)Session["SeIndex"] - 1].pkID + "", "rightname");
        Lb_right.Text = "【" + rightname + "】";

        Labmy.Visible = true;
        Label2.Visible = true;
        Labmy.Text = "你的答案是：" + RBL1.SelectedItem.Text.ToString();

        if (!RBL1.SelectedItem.Text.ToString().Equals(rightname))
        {
            Label2.Text = "对不起，你的答案错误！";
            //错误，则记录分数，选择题最后得分以80-session作为最后分数，判断题以20-session作为最后分数
            //未保证练习的有效性，分数以第一次选择的答案作为标准
            if (mList[(int)Session["SeIndex"] - 1].wtprotype == "选择题" && ((int)Session["IsEqual"] != ((int)Session["SeIndex"] - 1)))
            {
                Session["selMark"] = (int)Session["selMark"] + 2;
            }
            else if (mList[(int)Session["SeIndex"] - 1].wtprotype == "判断题" && ((int)Session["IsEqual"] != ((int)Session["SeIndex"] - 1)))
            {
                Session["PanDMark"] = (int)Session["PanDMark"] + 1;
            }
        }
        else
        {
            Label2.Text = "正确！";

        }
        if ((int)Session["SeIndex"] == 20 && mList[(int)Session["SeIndex"] - 1].wtprotype == "判断题")
        {
            Finishtimu.Visible = true;
            Finishtimu.Text = "你已经完成该份练习，现在可以提交！";

        }
        Session["IsEqual"] = (int)Session["SeIndex"] - 1;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //下一题
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            return;
        }
        if (((int)Session["IsEqual"] != ((int)Session["SeIndex"] - 1)) && ((List<Wtproblem>)Session["ProSeList"])[(int)Session["SeIndex"] - 1].wtprotype == "选择题")
        {
            Session["selMark"] = (int)Session["selMark"] + 2;
        }
        else if (((int)Session["IsEqual"] != ((int)Session["SeIndex"] - 1)) && ((List<Wtproblem>)Session["ProSeList"])[(int)Session["SeIndex"] - 1].wtprotype == "判断题")
        {
            Session["PanDMark"] = (int)Session["PanDMark"] + 1;
        }
        Labmy.Visible = false;
        Label2.Visible = false;
        Finishtimu.Visible = false;
        if (Session["SeIndex"] != null)
        {

            Session["SeIndex"] = (int)Session["SeIndex"] + 1;
            if ((int)Session["SeIndex"] > 40 && ((List<Wtproblem>)Session["ProSeList"])[(int)Session["SeIndex"] - 2].wtprotype == "选择题")
            {
                timu.Text = "判断题";
                shuoming.Text = "共20题判断题，每题1分！(提醒：在做题过程中请勿刷新页面！)";

                mList = getListPro("判断题", 20);
                Session["ProSeList"] = mList;
                Session["SeIndex"] = 1;
                Session["IsEqual"] = -1;
                LbTitle.Text = "第" + ((int)Session["SeIndex"]) + "题：" + mList[(int)Session["SeIndex"] - 1].wtproname;

                RBL1.DataTextField = "wtanname";
                RBL1.DataValueField = "wtanname";
                RBL1.DataSource = SQL.GetDataTable("wtanswer", "select wtanname from wtanswer where problemID=" + mList[(int)Session["SeIndex"] - 1].pkID + "");
                RBL1.DataBind();

                Lb_right.Text = "【请先选择答案！】";

                return;
            }
            if ((int)Session["SeIndex"] == 21 && ((List<Wtproblem>)Session["ProSeList"])[(int)Session["SeIndex"] - 2].wtprotype == "判断题")
            {
                Finishtimu.Visible = true;
                Finishtimu.Text = "你已经完成该份练习，现在可以提交！";
                LinkButton1.Enabled = false;
                LinkButton3.Visible = true;

                return;
            }
        }
        else
        {
            return;
        }
        if (Session["ProSeList"] != null)
        {
            mList = (List<Wtproblem>)Session["ProSeList"];
        }
        LbTitle.Text = "第" + ((int)Session["SeIndex"]) + "题：" + mList[(int)Session["SeIndex"] - 1].wtproname;

        RBL1.DataTextField = "wtanname";
        RBL1.DataValueField = "wtanname";
        RBL1.DataSource = SQL.GetDataTable("wtanswer", "select wtanname from wtanswer where problemID=" + mList[(int)Session["SeIndex"] - 1].pkID + "");
        RBL1.DataBind();

        Lb_right.Text = "【请先选择答案！】";
    }

    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        //提交练习，说明，这里的学号，在这个系统中就是登陆的用户名
        if (Session["stuproType"] != null && Session["PanDMark"] != null && Session["selMark"] != null && Session["username"] != null)
        {
            Wtmark mWtmark = new Wtmark();
            mWtmark.markm = Session["stuproType"] + "科目练习";
            mWtmark.markrigfa = 20 - (int)Session["PanDMark"];
            mWtmark.marksingse = 80 - (int)Session["selMark"];
            mWtmark.papertest = DateTime.Now.ToString("yyyy-MM-dd-HH-mm") + "-" + Session["username"];
            mWtmark.stunumber = Convert.ToString(Session["username"]);
            mWtmark.totalmark = mWtmark.markrigfa + mWtmark.marksingse;
            mWtmark.StudentName = SQL.GetData("select RealName from wtuser where wtusername = '" + mWtmark.stunumber + "'", "RealName");
            if (bWtmark.Insert(mWtmark, false))
            {
                MagicAjax.AjaxCallHelper.WriteAlert("你的总成绩是：" + mWtmark.totalmark + "，你可以查看你该次的练习成绩");
                LinkButton3.Enabled = false;
                RBL1.Enabled = false;
                CookieOper.CleanSession("ProSeList");
                CookieOper.CleanSession("SeIndex");
                Session["selMark"] = 0;
                Session["PanDMark"] = 0;
                Session["IsEqual"] = -1;
            }
        }
        else
        {
            CookieOper.CleanSession("username");
            CookieOper.CleanSession("usertype");
            CookieOper.CleanSession("stuproType");
            CookieOper.CleanSession("ProSeList");
            CookieOper.CleanSession("SeIndex");
            CookieOper.CleanSession("selMark");
            CookieOper.CleanSession("PanDMark");
            CookieOper.CleanSession("IsEqual");
            JScript.AlertAndRedirect("系统出现错误，请重新登录！", "leks.aspx");
        }
    }
}
